import React, { Component } from "react";
import "./index.css";

export default class Footer extends Component {
  handlerChange = (checked) => {
    this.props.checkAllTodo(checked);
  };

  deleteAllTodo = () => {
    this.props.deleteAllTodo();
  };

  render() {
    const { todos } = this.props;

    const allCount = todos.length;
    const doneCount = todos.reduce((pre, current) => {
      return pre + (current.done ? 1 : 0);
    }, 0);

    return (
      <div className="todo-footer">
        <label>
          <input
            type="checkbox"
            checked={allCount === doneCount && allCount !== 0 ? true : false}
            onChange={(e) => {
              this.handlerChange(e.target.checked);
            }}
          />
        </label>
        <span>
          <span>已完成{doneCount}</span> / 全部{allCount}
        </span>
        <button onClick={this.deleteAllTodo} className="btn btn-danger">
          清除已完成任务
        </button>
      </div>
    );
  }
}
